package com.nip.tranning.Activity;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.nip.trainning.Common.Constant;
import com.nip.trainning.DataTransferObject.PersonDataTable;
import com.nip.trainning.DataTransferObject.PersonEntity;
import com.nip.trainning.SQLiteProject.R;

public class ShowPeopleActivity extends Activity {

	private ListView lvPeople;
	private ArrayList<PersonEntity> listPeople;
	private String path;
	private ArrayAdapter<String> adapter;
	private PersonDataTable _personDataTable;
	ArrayList<String> listSource;
	private int _indexPeopleDelete;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.peoples_activity);
		// Get listview control
		lvPeople = (ListView) findViewById(R.id.lvPeople);
		registerForContextMenu(lvPeople);
		Bundle bundle = getIntent().getExtras();
		path = bundle.getString(Constant.INTENT_CODE_SEND_PATH);
		_personDataTable = new PersonDataTable(getApplication(), path);
		this.listPeople = (ArrayList<PersonEntity>) bundle
				.getSerializable(Constant.INTENT_CODE_SEND_LIST_PEOPLE);

		if (listPeople == null) {
			Log.w("Error", "listPeople null");
		}
		this.listSource = new ArrayList<String>();
		int i = 0;
		for (PersonEntity personEntity : listPeople) {
			listSource.add(personEntity.toString());
		}

		String[] list = { "a", "b", "c" };
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, listSource);
		lvPeople.setAdapter(adapter);
		lvPeople.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplication(),
						UpdatePeopleActivity.class);
				PersonEntity person = listPeople.get(arg2);
				Bundle bundle = new Bundle();
				bundle.putSerializable(Constant.INTENT_CODE_SEND_PEOPLE_UPDATE,
						person);
				bundle.putString(Constant.INTENT_CODE_SEND_PATH, path);
				i.putExtras(bundle);
				startActivityForResult(i,
						Constant.INTENT_CODE_REQUEST_PEOPLE_UPDATE);
				
			}
		});
		
		lvPeople.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				_indexPeopleDelete = arg2;
				return false;
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == Constant.INTENT_CODE_REQUEST_PEOPLE_UPDATE
				&& resultCode == Constant.INTENT_CODE_RESULT_PEOPLE_UPDATE) {
			data.getBooleanExtra(Constant.INTENT_CODE_SEND_PEOPLE_UPDATE, false);
			if (data.getBooleanExtra(Constant.INTENT_CODE_SEND_PEOPLE_UPDATE,
					false)) {
				this.listPeople = (ArrayList<PersonEntity>) _personDataTable
						.GetAllPersonInfomation();

				ArrayList<String> listSource = new ArrayList<String>();
				int i = 0;
				for (PersonEntity personEntity : listPeople) {
					listSource.add(personEntity.toString());
					i++;
				}
				this.listSource.clear();
				this.listSource.addAll(listSource);
				adapter.notifyDataSetChanged();
			}
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.people_context_menu, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.ctmDelete:
			AlertDialog.Builder dialog = new AlertDialog.Builder(
					ShowPeopleActivity.this);
			dialog.setTitle("NIPTeam");
			dialog.setMessage("Do you want to delete "
					+ this.listPeople.get(_indexPeopleDelete).toString() + " ?");
			dialog.setNegativeButton("NO",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub
							arg0.cancel();
						}
					});

			dialog.setPositiveButton("OK",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub

							try {

								_personDataTable.DeletePeople(listPeople.get(
										_indexPeopleDelete).getPersonID());

								ArrayList<String> listSource = new ArrayList<String>();
								int i = 0;
								
								listSource.remove(_indexPeopleDelete);

//								listSource.clear();
//								listSource.addAll(listSource);
								adapter.notifyDataSetChanged();
								_indexPeopleDelete = 0;
								
							} catch (Exception e) {
								Log.w("Exception", e.getMessage());
							}
						}
					});
			dialog.show();
			break;

		default:
			break;
		}
		return super.onContextItemSelected(item);
	}
}
